// 验证用户跳往用户中心链接权限
import {ciipWebUrl} from '~/../config/server.url.conf'
import {validateUserAuthor} from '~/api'
interface Params {
    that: any
    path?: string
    disableStr?: string
}
export default class ValidateLoginConfirm {
    url: string = ''
    msg: string = ''
    that: any = ''
    path: string = ''
    btnTitile: string = '立即前往'
    disableStr: string = ''
    constructor (parms: Params) {
        this.that = parms.that
        this.path = parms.path || ''
        this.disableStr = parms.disableStr || ''
    }
    async init () {
        await this.handleJump()
    }
    async handleJump () {
        const that: any = this.that
        this.setDisable(true)
        const u: any = that.$store.state.userinfo
        if (u.id) {
            if (u.selectRoleType === 'PERSON') {
                const status = u.realStatus
                if (status === 'REAL_STATUS_PASS') {
                    window.location.href = ciipWebUrl + this.path
                    return false
                } else {
                    this.msg = '个人用户实名认证未通过'
                    this.url = ciipWebUrl + '/account/safe'
                }
            } else {
                this.msg = '您当前非个人身份，请前往用户中心切换身份'
                this.url = ciipWebUrl
            }
        } else {
            this.msg = '您尚未登录，登录即可前往'
            this.url = ciipWebUrl + '/login?redirectUrl=' + window.location.href
            this.btnTitile = '立即登录'
        }
        this.setDisable(false)
        await this.validateLoginConfirm()
    }
    async validateLoginConfirm () {
        const that = this.that
        const confirm = await that.$confirm(this.msg, '提示', {
            confirmButtonText: this.btnTitile,
            cancelButtonText: '取消',
            customClass: 'qiao-confirm',
            center: true
        })
        if (confirm && this.url) {
            window.location.href = this.url
        }
    }
    async validateLoginConfirmForParams (msg: string, btnTitile: string, url: string) {
        this.msg = msg
        this.btnTitile = btnTitile
        this.url = url
        await this.validateLoginConfirm()
    }
    setDisable (flag: boolean) {
        const that = this.that
        const disableStr: string = this.disableStr
        if (disableStr) {
            that[disableStr] = flag
        }
    }
    async toJumpUc (mark: string, path: string = '', linkName: string = '') {
        const that = this.that
        let suffix: string = ''
        if (mark === 'SERVICE' || mark === 'DEMAND') {
            suffix = '?t=second'
        }
        that.disabled = true
        const uuid = that.$store.state.token
        const res: any = await validateUserAuthor(mark, uuid, path)
        const data = res.data
        if (data.success) {
            window.location.href = ciipWebUrl + path + suffix
        } else {
            let msg: string = data.msg
            let url: string = ''
            let btn: string = ''
            switch (data.data) {
                case 'USER_NOT_LOGIN':
                    btn = '立即登录'
                    msg = '您尚未登录，登录即可前往'
                    url = ciipWebUrl + '/login?redirectUrl=' + window.location.href
                    break
                case 'CHANGE_ROLE':
                    btn = '前往用户中心'
                    msg = '只有个人用户能成为专家'
                    url = ciipWebUrl
                    break
                case 'PERSON_REAL_AUTH_NOPASS':
                    btn = '立即前往'
                    msg = '个人用户实名认证未通过'
                    url = ciipWebUrl + '/account/safe'
                    break
                case 'ENTERPRISE_SHOP_NOTOPEN':
                    btn = '立即前往'
                    msg = '企业店铺未开通'
                    url = ciipWebUrl + '/business/shop'
                    break
                case 'ENTERPRISE_REAL_AUTH_NOPASS':
                    btn = '立即前往'
                    msg = '企业认证未通过'
                    url = ciipWebUrl + '/account/authentication'
                    break
                case 'NO_SETTING_AUTH': {
                    const isPerson: boolean = that.$store.state.userinfo.selectRoleType === 'PERSON'
                    btn = isPerson ? '前往创建' : '前往用户中心'
                    url = ciipWebUrl
                    if (isPerson) {
                        msg = '请创建企业账号并开通店铺后再发布服务'
                        url += '/account/addcreatecompany'
                    } else {
                        msg = `您暂时没有${linkName}的权限，如有疑问，请联系企业管理员`
                    }
                    break
                }
                case 'AUTH_SUCCESS':
                    btn = '立即前往'
                    msg = '是否继续前往访问'
                    url = ciipWebUrl + path + '?t=second'
                    break
            }
            this.validateLoginConfirmForParams(msg, btn, url)
        }
        that.disabled = false
    }
}
